#!/usr/bin/env python3
"""
Script to recreate the OpenMemory collection with updated vector dimensions.
This script should be run inside the OpenMemory Docker container.
"""

import sys
import os
import logging
from pathlib import Path

# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

def recreate_collection():
    """
    Recreate the collection with updated vector dimensions.
    """
    try:
        # Import the memory utilities
        from app.utils.memory import get_memory_client, reset_memory_client
        
        logger.info("Starting OpenMemory collection recreation...")
        
        # Reset any cached memory client
        logger.info("Resetting cached memory client...")
        reset_memory_client()
        
        # Get a fresh memory client
        logger.info("Initializing memory client with new 1024-dimensional configuration...")
        memory_client = get_memory_client()
        
        if memory_client is None:
            logger.error("Failed to initialize memory client")
            return False
        
        # Reset the memory store to recreate collection
        logger.info("Recreating collection with 1024-dimensional vectors...")
        memory_client.reset()
        
        logger.info("✅ Collection recreation completed successfully!")
        logger.info("The collection now supports 1024-dimensional vectors.")
        return True
        
    except Exception as e:
        logger.error(f"❌ Error during collection recreation: {e}")
        return False

def main():
    """
    Main execution function.
    """
    logger.info("OpenMemory Collection Recreation")
    logger.info("================================")
    logger.info("Recreating collection with 1024-dimensional vectors...")
    logger.info("WARNING: This will delete all existing memories!")
    
    success = recreate_collection()
    
    if success:
        logger.info("\n🎉 Collection successfully recreated with 1024-dimensional vectors!")
    else:
        logger.error("\n💥 Collection recreation failed.")
        sys.exit(1)

if __name__ == "__main__":
    main()